# coding=utf-8
import codecs
import json
import datetime
import MySQLdb

def read_file(fpath): 
    BLOCK_SIZE = 1024 
    with codecs.open(fpath, 'r+') as f:
        while True: 
            block = f.read(BLOCK_SIZE) 
            if block: 
                yield block 
            else: 
                return
                
base_file = 'C:/Users/huhai/Desktop/name.json'

t1 = datetime.datetime.now()

s = ''
for i in read_file(base_file):
    s += i
t2 = datetime.datetime.now()
d = json.loads(s)
t3 = datetime.datetime.now()

# d = {1:1,2:2}
_conn = MySQLdb.connect(user='root', passwd='oseasydads_db',
                host='127.0.0.1', port=3100, charset='utf8')
_conn.select_db('test')
for k in d:
    try:
        sql = """INSERT INTO `%s` (`name`, `value`) VALUES('%s', %s);
              """ % ('xxx', k, d[k])

        _conn.cursor().execute(sql)
    except:
        print k, d[k]

t4 = datetime.datetime.now()
_conn.commit()
print t1, t2, t3, t4